// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database()
  
  try {
    // 存储分析结果到数据库
    const res = await db.collection('medical_analysis').add({
      data: {
        _openid: wxContext.OPENID,
        fileIDs: event.fileIDs,
        tips: event.tips,
        createTime: db.serverDate(),
        updateTime: db.serverDate(),
        account:event.account
      }
    })
    
    return {
      success: true,
      recordId: res._id
    }
  } catch (err) {
    console.error('存储失败:', err)
    return {
      success: false,
      error: err.message
    }
  }
}